local GameDB = import(".GameDB")

function GameDB:getAchievementConfig(achId)
    local achievementConfig = self:getCachedValue("achievement",achId)
    return achievementConfig
end

function GameDB:getAchievementConfigByType(t)
    if not t then
        return nil
    end

    return self:getCachedValues("achievement",function(v)
        return v.type==tostring(t)
    end)
end

function GameDB:quereAllAchievements()
    local achievementsConfig = self:getCachedTable("achievement") or {}
    return achievementsConfig
end

function GameDB:queryAchievementByGroup(group)
    local configs = self:getCachedValues("achievement",function(v)
        return tonumber(v.group) == tonumber(group)
    end)
    return configs
end

function GameDB:getWorldEventByNpcId(npcId)
    --TODO 待新设计
    --local sql = string.format("SELECT * FROM world_event WHERE npcLocation = %d",tonumber(npcId))
    --local worldEventConfig = self:query(sql)
    --return worldEventConfig
    return nil
end

function GameDB:getWorldEvent()
    --TODO 待新设计
    --local worldEventConfig = self:getCachedTable("world_event") or {}
    --return worldEventConfig
    return {}
end

function GameDB:getAchievementGroup(group)
    --local sql = string.format("SELECT * FROM achievement WHERE group = %d",group)
    local achievementGroupConfig = self:getCachedValues("achievement",function(v) return v.group==tostring(group) end)
    return achievementGroupConfig
end

function GameDB:getAchievementType(t)
    --local sql = string.format("SELECT * FROM achievement WHERE (showType is null or showType<>1) and type = %d",t)
    local achievementGroupConfig = self:getCachedValues("achievement",
        function(v)
            return v.type == tostring(t) and v.showType~="1"
        end)
    return achievementGroupConfig
end

function GameDB:getWeeklyTask()
    --local sql = string.format("SELECT * FROM achievement WHERE time = 3")
    local delayTaskConfig = self:getCachedValues("achievement",
        function(v)
            return v.time=="3"
        end)
    return delayTaskConfig
end

function GameDB:queryBadges(orderFunc)
    return self:getCachedValues("badge",nil,orderFunc)
end

function GameDB:queryBadgesByType(t)
    return self:getCachedValues("badge",
        function(v)
            return v.type==tostring(t)
        end)
end

function GameDB:queryBadgeById(badgeId)
    return self:getCachedValue("badge",badgeId)
end

function GameDB:queryBadgeByAchId(achId)
    --self:querySingle("SELECT * FROM badge WHERE achievementIDs='" .. achId.."' or achievementIDs like '" .. achId..";%' or achievementIDs like '%;" .. achId.."' or achievementIDs like '%;" .. achId..";%'")
    return self:getCachedValue("badge",
        function(v)
            return string.find(v.achievementIDs,achId)~=nil
        end)
end

function GameDB:queryBadgeName(badgeId)
    local name = ""
    if badgeId then
        local badgeConfig = self:queryBadgeById(badgeId)
        if badgeConfig and badgeConfig.ID then
            name = L(badgeConfig.name)
        end
    end
    return name
end

function GameDB:queryBadgeQuality(badgeId)
    local quality = 0
    if badgeId then
        local badgeConfig = self:queryBadgeById(badgeId)
        if badgeConfig and badgeConfig.lv then
            quality = tonumber(badgeConfig.lv)
        end
    end
    return quality
end

--[[--
-- 查询成就名称
]]
function GameDB:getCollectionName(tid)
    if tid and tonumber(tid)>0 then
	    local collectionName = self:getAchievementConfig(tid).name
	    return collectionName
    else
        return ""
    end
end

function GameDB:getAchievementTypes()
    local achievementTypeConfig = self:getCachedValues("achievement_subkey")
    return achievementTypeConfig
end

function GameDB:getAchievementConfigByID(aId)
    local achievementConfig = self:getCachedValue("achievement",aId)
    return achievementConfig
end

function GameDB:getAchievements()
    local achievementsConfig = self:getCachedValues("achievement")
    return achievementsConfig
end 

function GameDB:getAchievementStatistics()
    local achievementStatisticsConfig = self:getCachedValues("achievement_statistics")
    return achievementStatisticsConfig
end 

function GameDB:getAchievementPointById(aId)    
    return checknumber(self:getAchievementConfigByID(aId).point)
end 

function GameDB:getAchievementStatisticsTypeConfig(aId)
    local ret = self:getCachedValue("achievement_class",aId)
    return ret
end 

return GameDB
